﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CiroGoruntuleme
{

    public class Rapor
    {
        public string MagazaAdi { get; set; }
        public int BelgeSayisi { get; set; }
        public int IadeSayisi { get; set; }
        public decimal SatisTutari { get; set; }
        public decimal IadeTutari { get; set; }
        public decimal NetTutar { get; set; }
    }

    public class Depo
    {
        public int No { get; set; }
        public string Adi { get; set; }
        public string Yol { get; set; }
    }

    public class Mikro
    {

        public Rapor OncekiGunSatisi(DateTime tarih, Depo dp)
        {
            Rapor rpr = new Rapor();
            rpr.MagazaAdi = dp.Adi;
            try
            {
                using (SqlConnection cn = new SqlConnection(Sessions.Get.CnnString))
                {
                    cn.Open();
                    using (SqlCommand cmd = new SqlCommand(string.Format("SELECT     po_Fisfatura AS Tip, COUNT(po_RECno) AS Sayi, SUM(po_BelgeToplam) AS Tutar FROM         O_{0}_{1} GROUP BY po_Fisfatura", tarih.Year.ToString() + tarih.Month.ToString("00") + tarih.Day.ToString("00"), dp.No.ToString("000")), cn))
                    {
                        SqlDataReader rd = cmd.ExecuteReader();
                        while (rd.Read())
                        {
                            if (Cevir.Parse<int>(rd[0]) == 0)
                            {
                                rpr.IadeSayisi = Cevir.Parse<int>(rd[1]);
                                rpr.IadeTutari = Cevir.Parse<decimal>(rd[2]);
                            }
                            else
                            {
                                rpr.BelgeSayisi += Cevir.Parse<int>(rd[1]);
                                rpr.SatisTutari += Cevir.Parse<decimal>(rd[2]);
                            }
                        }
                    }
                    cn.Close();
                }
            }
            catch (Exception ex)
            {
                Logging.GetLog.ErrorLog(ex);
            }
            rpr.NetTutar = rpr.SatisTutari - rpr.IadeTutari;
            return rpr;
        }

        public List<Depo> ListDepo()
        {
            List<Depo> _liste = new List<Depo>();
            using (SqlConnection cn = new SqlConnection(Sessions.Get.CnnString.Replace("_WORKDATA", "")))
            {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand("SELECT   dep_no, dep_adi FROM         DEPOLAR", cn))
                {
                    SqlDataReader rd = cmd.ExecuteReader();
                    while (rd.Read())
                    {
                        _liste.Add(new Depo()
                        {
                            Adi = rd.GetString(1),
                            No = rd.GetInt32(0),
                            Yol = ""
                        });
                    }
                }
                cn.Close();
            }
            return _liste;
        }
    }
}
